package cn.yr.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.search.bean.ArticleDocBean;
import cn.yr.search.service.ArticleElasticService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(
        bindings=@QueueBinding(
                value=@Queue(value="${mq.search.queue.add}",autoDelete="true"),
                exchange=@Exchange(value="${mq.search.exchange}",type= ExchangeTypes.DIRECT),
                key="${mq.search.routing-key.add}"
        )
)
public class SearchListener {

    @Autowired
    private ArticleElasticService articleElasticService;

    @RabbitHandler
    public void process(YrArticle article){
        log.info("Elastic Save 收到消息：{}",article);
        ArticleDocBean articleDocBean = new ArticleDocBean();
        BeanUtil.copyProperties(article,articleDocBean);
        articleElasticService.save(articleDocBean);
    }


}
